package message

import (
	"net/http"
	"strconv"

	dao2 "gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/dao"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/entity"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/model"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// ********************************************
// Func  ：查询操作员的站内信列表
// Method：GET
// Usage ：
// ********************************************
func QueryOperatorMessages(c *gin.Context) {

	pageSize, err1 := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	if err1 != nil {
		global.Log.Error("QueryOperatorMessages input param pagesize error:", zap.Any("error", err1))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}

	page, err2 := strconv.Atoi(c.DefaultQuery("page", "1"))
	if err2 != nil {
		global.Log.Error("QueryOperatorMessages input param page error:", zap.Any("error", err2))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}

	receiverId, err := strconv.ParseInt(c.DefaultQuery("receiverId", ""), 10, 64)
	if err != nil {
		global.Log.Error("QueryOperatorMessages receiverId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指名收件人"))
		return
	}

	readFlag, err := strconv.Atoi(c.DefaultQuery("readFlag", "0"))
	msgType, err := strconv.Atoi(c.DefaultQuery("msgType", "0"))

	total, msgs, err := dao2.QueryReceivedMsgList(page, pageSize, receiverId, readFlag, msgType)

	if err != nil {
		global.Log.Error("QueryAllMessages failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("查询站内信失败"))
	} else {

		type ReceiveMsgList struct { //列表查询输出
			Total int64
			List  []*entity.ReceivingMessage
		}

		output := &ReceiveMsgList{
			Total: total,
			List:  msgs,
		}

		c.JSON(http.StatusOK, model.SuccessObject("操作成功", output))
		global.Log.Info("QueryAllMessages success.", zap.Any("label list", output))
	}
}

// ********************************************
// Func  ：查询指定站内信详情
// Method：GET
// Usage ：
// ********************************************
func QueryMessageDetail(c *gin.Context) {
	deptId, err := strconv.ParseInt(c.DefaultQuery("id", ""), 10, 64)
	if err != nil {
		global.Log.Error("QueryLabelDetail id is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明查询站内信"))
		return
	}
	if deptId <= 0 {
		global.Log.Error("QueryLabelDetail deptid is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明查询站内信"))
		return
	}

	/*label, err := dao2.QueryLabelDetail(deptId)
	if err != nil {
		global.Log.Error("QueryLabelDetail failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("查询站内信详情失败"))
	} else {
		c.JSON(http.StatusOK, model.SuccessObject("操作成功", label))
		global.Log.Info("QueryLabelDetail success.", zap.Any("label detail", label))
	}*/
}

// ********************************************
// Func  ：添加新站内信
// Method：POST
// Usage ：
// ********************************************
func CreateNewMessage(title, body, sender string, senderId int64, receiverIds []int64) {
	/*
		var label entity.Label
		var err error
		label.Label = strings.TrimSpace(c.DefaultPostForm("label", "")) //标题
		if len(label.Label) <= 0 {
			global.Log.Error("AddNewLabel name is none.")
			c.JSON(http.StatusOK, model.Fail("站内信名称不能为空"))
			return
		}

		label.Author, err = strconv.ParseInt(c.DefaultPostForm("author", ""), 10, 64)
		label.Remark = c.DefaultPostForm("remark", "")

		newlabel, err := dao.CreateLabel(label)
		if err != nil {
			global.Log.Error("AddNewLabel failed.", zap.Any("error", err))
			c.JSON(http.StatusOK, model.Fail("添加站内信失败"))
		} else {
			c.JSON(http.StatusOK, model.SuccessObject("操作成功", newlabel))
			global.Log.Info("AddNewLabel success.", zap.Any("new label", newlabel))
		}

	*/
}
